import 'package:flutter/material.dart';
import '../../common/ScreenAdaper.dart';
import 'package:dio/dio.dart';
import '../../config/Config.dart';
import '../../widget/Tool.dart';

class ListPage extends StatefulWidget {
  Map arguments;
  ListPage({Key key,this.arguments}) : super(key: key);
  _ListPageState createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {

  List _data = [];
  @override
  void initState(){
    super.initState();
    _getDate();
  }

  _getDate() async{
    var apiUrl = "${Config.domain}api/book/search?keyword=${widget.arguments["keywords"]}";
    Response result=await Dio().get(apiUrl);
    var list=result.data;
    if(list['code'] == 1){
      setState(() {
        this._data = list["data"];
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("书籍搜索"),
        ),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 10,),
              Tool.TitleWidget(context,"搜索结果"),
              _listWidget(),
            ],
          ),
        )
    );
  }

  // 热点内容
  Widget _listWidget(){
    if(_data.length > 0) {
      return Column(
        children: _data.map((v) {
          return Column(
            children: [
              Container(
                margin: EdgeInsets.all(S.width(20.0)),
                child: GestureDetector(
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: S.width(200),
                        height: S.height(240),
                        child: Image.network(
                            "${v['Image']}", fit: BoxFit.cover),
                      ),
                      Expanded(
                        flex: 1,
                        child: Container(
                          height: S.height(240),
                          margin: EdgeInsets.only(left: 10),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text("${v['Name']}", maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(fontSize: S.size(30),
                                    fontWeight: FontWeight.bold),),
                              Container(
                                padding: EdgeInsets.only(top: 1, bottom: 1),
                                child: Text("作者：${v['Author']}", maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(fontSize: S.size(28),
                                        color: Colors.black54)),
                              ),
                              Text("${v['Info']}", maxLines: 5,
                                overflow: TextOverflow.ellipsis,),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                  onTap: () {
                    Navigator.pushNamed(context, "/book",arguments:{"bookId":v["BookId"]});
                  },
                ),
              ),
              Divider(height: 1,)
            ],
          );
        }).toList(),
      );
    } else {
      return Tool.Loading();
    }
  }
}